package SecondSaturday;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class InstanceTest extends JFrame {

	private JPanel contentPane;
	private JLabel lblStringA;
	private JLabel lblStringB;
	private JLabel lblStringE;
	private JLabel lblPointC;
	private JLabel lblPointD;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JButton button;
	private JButton button_1;
	private JButton button_2;
	private JButton button_3;
	private JScrollPane scrollPane;
	private JTextArea result;
	private String a;
	private Point c, d;
	private JLabel lblNewLabel;
	private JTextField textField_3;
	private JButton btnNewButton;
	private JButton btnC;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					InstanceTest frame = new InstanceTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public InstanceTest() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 631);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblStringA());
		contentPane.add(getLblStringB());
		contentPane.add(getLblStringE());
		contentPane.add(getLblPointC());
		contentPane.add(getLblPointD());
		contentPane.add(getTextField());
		contentPane.add(getTextField_1());
		contentPane.add(getTextField_2());
		contentPane.add(getButton());
		contentPane.add(getButton_1());
		contentPane.add(getButton_2());
		contentPane.add(getButton_3());
		contentPane.add(getScrollPane());
		contentPane.add(getLblNewLabel());
		contentPane.add(getTextField_3());
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnC());
	}
	private JLabel getLblStringA() {
		if (lblStringA == null) {
			lblStringA = new JLabel("String a = new String(*);");
			lblStringA.setBounds(12, 14, 142, 15);
		}
		return lblStringA;
	}
	private JLabel getLblStringB() {
		if (lblStringB == null) {
			lblStringB = new JLabel("String b = a;");
			lblStringB.setBounds(12, 38, 142, 15);
		}
		return lblStringB;
	}
	private JLabel getLblStringE() {
		if (lblStringE == null) {
			lblStringE = new JLabel("String e = new String(*);");
			lblStringE.setBounds(12, 60, 142, 15);
		}
		return lblStringE;
	}
	private JLabel getLblPointC() {
		if (lblPointC == null) {
			lblPointC = new JLabel("Point c = new Point();");
			lblPointC.setBounds(12, 85, 142, 15);
		}
		return lblPointC;
	}
	private JLabel getLblPointD() {
		if (lblPointD == null) {
			lblPointD = new JLabel("Point d = c.clone();");
			lblPointD.setBounds(12, 125, 142, 15);
		}
		return lblPointD;
	}
	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setText("a와 같은 값 입력");
			textField.setColumns(10);
			textField.setBounds(166, 102, 116, 21);
		}
		return textField;
	}
	private JTextField getTextField_1() {
		if (textField_1 == null) {
			textField_1 = new JTextField();
			textField_1.setText("a와 같은 값 입력");
			textField_1.setColumns(10);
			textField_1.setBounds(166, 57, 116, 21);
		}
		return textField_1;
	}
	private JTextField getTextField_2() {
		if (textField_2 == null) {
			textField_2 = new JTextField();
			textField_2.setText("아무거나 입력");
			textField_2.setColumns(10);
			textField_2.setBounds(166, 11, 116, 21);
		}
		return textField_2;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("a==b?");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					a = new String(textField_2.getText());
					String b = a;
					
					result.append("a = " + a + ", b = " + b + "\n");
					if (a==b) result.append("a == b\n"); 
					else result.append("a != b\n");
					if (a.equals(b)) result.append("a.equals(b)\n");
					else result.append("!a.equals(b)\n");
					result.append("--------------------\n");
					button_1.setEnabled(true);
				}
			});
			button.setBounds(325, 10, 97, 23);
		}
		return button;
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("a==e?");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String e = new String(textField_1.getText());
					
					result.append("a = " + a + ", e = " + e + "\n");
					if (a==e) result.append("a == e\n");
					else result.append("a != e\n");
					if (a.equals(e)) result.append("a.equals(e)\n");
					else result.append("!a.equals(e)\n");
					result.append("--------------------\n");
					button_2.setEnabled(true);
				}
			});
			button_1.setEnabled(false);
			button_1.setBounds(325, 56, 97, 23);
		}
		return button_1;
	}
	private JButton getButton_2() {
		if (button_2 == null) {
			button_2 = new JButton("c==d?");
			button_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
/*					c = new Point();
					c.original = textField_2.getText();
					d = (Point) c.clone();*/
					c = new Point();
					c.original = textField_2.getText();
					d = (Point) c.clone();

					result.append("c = " + c + ", d = " + d + "\n");
					if (c==d) result.append("c == d\n");
					else result.append("c != d\n");
					if (c.equals(d)) result.append("c.equals(d)\n");
					else result.append("!c.equals(d)\n");
					
					result.append("c.original = " + c.original + ", d.original = " + d.original + "\n");
					if (c.original==d.original) result.append("c.original == d.original\n");
					else result.append("c.original != d.original\n");
					if ((c.original).equals(d.original)) result.append("(c.original).equals(d.original)\n");
					else result.append("!(c.original).equals(d.original)\n");
					result.append("--------------------\n");
					
/*					result.append("a = " + a + ", c = " + c + "\n");
					if (a==(String)c) result.append("a == c\n");
					else result.append("a != c\n");
					if (a.equals(c)) result.append("a.equals(c)\n");
					else result.append("!a.equals(c)\n");
					result.append("--------------------\n");*/

					textField_3.setEnabled(true);
					btnC.setEnabled(true);
					btnNewButton.setEnabled(true);
				}
			});
			button_2.setEnabled(false);
			button_2.setBounds(325, 101, 97, 23);
		}
		return button_2;
	}
	private JButton getButton_3() {
		if (button_3 == null) {
			button_3 = new JButton("a==d?");
			button_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					result.append("a = " + a + ", d = " + d.original + "\n");
					if (a==d.original) result.append("c == d.original\n");
					else result.append("c != d.original\n");
					if (a.equals(d)) result.append("a.equals(d.original)\n");
					else result.append("!a.equals(d.original)\n");
					result.append("--------------------\n");
				}
			});
			button_3.setEnabled(false);
			button_3.setBounds(325, 340, 97, 23);
		}
		return button_3;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 154, 270, 429);
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}
	private JTextArea getTextArea() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("c.original = *;");
			lblNewLabel.setBounds(12, 105, 142, 15);
		}
		return lblNewLabel;
	}
	private JTextField getTextField_3() {
		if (textField_3 == null) {
			textField_3 = new JTextField();
			textField_3.setEnabled(false);
			textField_3.setBounds(306, 156, 116, 21);
			textField_3.setColumns(10);
		}
		return textField_3;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("d값 변경");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						d.original = textField_3.getText();
						
						result.append("c = " + c + ", d = " + d + "\n");
						if (c==d) result.append("c == d\n");
						else result.append("c != d\n");
						if (c.equals(d)) result.append("c.equals(d)\n");
						else result.append("!c.equals(d)\n");
						
						result.append("c.original = " + c.original + ", d.original = " + d.original + "\n");
						if (c.original==d.original) result.append("c.original == d.original\n");
						else result.append("c.original != d.original\n");
						if ((c.original).equals(d.original)) result.append("(c.original).equals(d.original)\n");
						else result.append("!(c.original).equals(d.original)\n");
						result.append("--------------------\n");
						button_3.setEnabled(true);
					} catch (Exception e) {
						
					}
				}
			});
			btnNewButton.setEnabled(false);
			btnNewButton.setBounds(325, 230, 97, 23);
		}
		return btnNewButton;
	}
	private JButton getBtnC() {
		if (btnC == null) {
			btnC = new JButton("c값 변경");
			btnC.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					c.original = textField_3.getText();
					
					result.append("c = " + c + ", d = " + d + "\n");
					if (c==d) result.append("c == d\n");
					else result.append("c != d\n");
					if (c.equals(d)) result.append("c.equals(d)\n");
					else result.append("!c.equals(d)\n");
					
					result.append("c.original = " + c.original + ", d.original = " + d.original + "\n");
					if (c.original==d.original) result.append("c.original == d.original\n");
					else result.append("c.original != d.original\n");
					if ((c.original).equals(d.original)) result.append("(c.original).equals(d.original)\n");
					else result.append("!(c.original).equals(d.original)\n");
					result.append("--------------------\n");
					button_3.setEnabled(true);
				}
			});
			btnC.setEnabled(false);
			btnC.setBounds(325, 197, 97, 23);
		}
		return btnC;
	}
}

class Point implements Cloneable {
	String original;
	public Object clone() {
		Object o = null;
		try {
			o = super.clone();
		}
		catch(Exception ex) {}
		return o;
	}
}
